<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET'){	
	
	$forms = $_GET;
	$company_name = $_GET["company_name"];
	$katakana = $_GET["katakana"];
	$branch_name = $_GET["branch_name"];
	$sal_manager = $_GET["sal_manager"];
	$r_date1 = $_GET["r_date1"];
	$r_date2 = $_GET["r_date2"];
	$u_date1 = $_GET["u_date1"];
	$u_date2 = $_GET["u_date2"];
	
	$current = 1;
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}
	
	$where = '';
	$query = '';
	if($company_name != null){
		$where = $where." and company_name like '%".$company_name."%'";	
		$query .="&company_name=".$company_name;
	}
	if($katakana != null){
		$where = $where." and katakana like '%".$katakana."%'";	
		$query .="&katakana=".$katakana;
	}
	if($branch_name != null){
		$where = $where." and branch_name like '%".$branch_name."%'";
		$query .="&branch_name=".$branch_name;
	}
	if($sal_manager != null){
		$where = $where." and sal_manager like '%".$sal_manager."%'";	
		$query .="&sal_manager=".$sal_manager;
	}
	if($r_date1 != null && $r_date2 != null){
		$where = $where." and r_date BETWEEN '".$r_date1."' and '".$r_date2."'";
		$query .="&r_date1=".$r_date1."&r_date2=".$r_date2;
	}
	if($r_date1 != null && $r_date2 == null){
		$where = $where." and r_date >= '".$r_date1."'";
		$query .="&r_date1=".$r_date1;
	}
	if($r_date1 == null && $r_date2 != null){
		$where = $where." and r_date <= '".$r_date2."'";
		$query .="&r_date2=".$r_date2;
	}
	
	if($u_date1 != null && $u_date2 != null){
		$where = $where." and u_date BETWEEN '".$u_date1."' and '".$u_date2."'";	
		$query .="&u_date1=".$u_date1."&u_date2=".$u_date2;
	}
	if($u_date1 != null && $u_date2 == null){
		$where = $where." and u_date >= '".$u_date1."'";
		$query .="&u_date1=".$u_date1;
	}
	if($u_date1 == null && $u_date2 != null){
		$where = $where." and u_date <= '".$u_date2."'";
		$query .="&u_date2=".$u_date2;
	}
	$_SESSION['consumer_index_search'] = "page=$current".$query;
	$sql =  "SELECT * FROM p_consumer WHERE 1 = 1 $where order by u_date desc LIMIT $st, ".PAGE_SIZE;
	$sqlcount ="SELECT COUNT(*) as cnt FROM p_consumer WHERE 1=1 ".$where;
	$db = new Class_DB;		
	$res = $db->query($sql);
	
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
		$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
		$consumerlist[] = $row;
	}
	
	$cnt = 0;
	$db->query( $sqlcount );
	$row = $db->fetch();
	$cnt = $row['cnt'];
	
	$smarty->assign('page', getPage($current, $cnt,$query));
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
}



$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->assign('consumerlist', $consumerlist);

$smarty->assign('cnt', $cnt);

$smarty->display('web-admin/consumer_index.html');


?>